package com.isunland.managesystem.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.isunland.managesystem.adapter.CustomerVisitHintAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.base.BaseOriginal;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.BaseParams;
import com.isunland.managesystem.entity.rCustomerNeedListMain;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2017/7/11 13:54
 */
public class CustomerVisitHintListFragment extends BaseListFragment {
  private ArrayList<rCustomerNeedListMain> mList;
  private CustomerVisitHintAdapter mAdapter;

  @Override public void initData() {
    super.initData();
    mList = new ArrayList<>();
    mAdapter = new CustomerVisitHintAdapter(mActivity, mList);
  }

  @Override public void initView(View view, Bundle savedInstanceState) {
    super.initView(view, savedInstanceState);
    setListAdapter(mAdapter);
  }

  @Override public String getUrl() {
    return ApiConst.URL_QUERY_LIST_CUSTOMER_VISIT_HINT;
  }

  @Override public HashMap<String, String> getparameters() {
    ParamsNotEmpty params = new ParamsNotEmpty();
    params.put("memberCode", mCurrentUser.getMemberCode());
    params.put("orderField", "order_no");
    params.put("orderSeq", "asc");
    params.put("queryAll", String.valueOf(true));
    return params.getParams();
  }

  @Override public void resolveResponse(String json) {
    BaseOriginal<rCustomerNeedListMain> result =
        new Gson().fromJson(json, new TypeToken<BaseOriginal<rCustomerNeedListMain>>() {
        }.getType());
    mList.clear();
    mList.addAll(result.getRows());
    mAdapter.notifyDataSetChanged();
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    if (position < mListview.getHeaderViewsCount()) return;
    rCustomerNeedListMain item = mAdapter.getItem(position - l.getHeaderViewsCount());
    if (item == null) return;
    BaseVolleyActivity.newInstance(this, CustomerVisitHintDetailActivity.class,
        new BaseParams<rCustomerNeedListMain>().setItem(item), 0);
  }
}
